/*
 * Author: Alvin
 * Modified By: Alvin
 * Created Date: 2024-05-13 15:43:36
 * Last Modified: 2024-05-14 09:15:03
 * Description:
 */
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:get/get_connect/http/src/utils/utils.dart';
import 'package:yt_devops/styles/styles.dart';

class FormConfigModel {
  List<FormConfigGroupModel> groupList;

  FormConfigModel({
    required this.groupList,
  });

  factory FormConfigModel.fromJsonList(List<Map<String, dynamic>> jsonList) {
    return FormConfigModel(
      groupList: jsonList.map((e) => FormConfigGroupModel.fromJson(e)).toList(),
    );
  }

  factory FormConfigModel.fromWebJsonList(List<Map<String, dynamic>> jsonList) {
    int groupCount = jsonList.last['group'] ?? 0;
    List<FormConfigGroupModel> groupList = List.generate(groupCount + 1, (index) {
      FormConfigGroupModel groupModel = FormConfigGroupModel(groupTitle: '', childrens: []);
      for (var element in jsonList) {
        if (element['group'] == index) {
          groupModel.childrens.add(SingleFormItemModel.fromWebJson(element));
        }
      }
      return groupModel;
    });

    return FormConfigModel(
      groupList: groupList,
    );
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['groupList'] = groupList;
    return data;
  }
}

class FormConfigGroupModel {
  String groupTitle;
  List<SingleFormItemModel> childrens;

  FormConfigGroupModel({
    required this.groupTitle,
    required this.childrens,
  });

  factory FormConfigGroupModel.fromJson(Map<String, dynamic> json) {
    return FormConfigGroupModel(
      groupTitle: json['groupTitle'] ?? '',
      childrens: List.from(json['childrens'].map((e) => SingleFormItemModel.fromJson(e)).toList()),
    );
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['groupTitle'] = groupTitle;
    data['childrens'] = childrens;
    return data;
  }
}

class SingleFormItemModel {
  bool isRequired;
  // 标题
  String title;
  // 副标题
  String? subTitle;
  // 提示文字
  String? hintTextString;
  // 键盘类型
  TextInputType? keyboardType;
  // 输入框类型
  int type;
  // 选择框的数据 /
  List? selectList;
  // 最大值
  double? maxValue;
  // 最小值
  double? minValue;
  // 输入框的值
  String? value;
  // 右侧的widget
  Widget? rightSlot;
  // 左侧的widget
  Widget? leftSlot;
  double? titleWidth;
  bool? isCanEdit; //是否可以响应输入
  int? maxLines; //最大行数
  Widget? suffixIcon; // textfiled后缀图标
  int? maxLength; // 可输入的最大长度
  int? maxPhotoCount; // 可上传的最大图片数量

  SingleFormItemModel({
    required this.isRequired,
    required this.title,
    this.subTitle,
    this.hintTextString,
    this.keyboardType,
    required this.type,
    this.selectList,
    this.maxValue,
    this.minValue,
    this.value,
    this.rightSlot,
    this.leftSlot,
    this.titleWidth,
    this.isCanEdit,
    this.maxLines,
    this.suffixIcon,
    this.maxLength,
    this.maxPhotoCount,
  });

  factory SingleFormItemModel.fromJson(Map<String, dynamic> json) {
    return SingleFormItemModel(
      isRequired: json['isRequired'],
      title: json['title'],
      subTitle: json['subTitle'],
      hintTextString: json['hintTextString'],
      keyboardType: json['keyboardType'],
      type: json['type'],
      selectList: json['selectList'],
      maxValue: json['maxValue'] ?? 0,
      minValue: json['minValue'] ?? 0,
      value: json['value'],
      rightSlot: json['rightSlot'],
      leftSlot: json['leftSlot'],
      titleWidth: json['titleWidth'],
      isCanEdit: json['isCanEdit'],
      maxLines: json['maxLines'],
      suffixIcon: json['suffixIcon'],
      maxLength: json['maxLength'],
      maxPhotoCount: json['maxPhotoCount'],
    );
  }

  factory SingleFormItemModel.fromWebJson(Map<String, dynamic> json) {
    SingleFormItemModel model = SingleFormItemModel(
      isRequired: json['isRequired'],
      title: json['title'],
      subTitle: json['subTitle'],
      hintTextString: json['hintTextString'],
      keyboardType: json['keyboardType'],
      type: json['type'],
      selectList: json['selectList'],
      maxValue: json['maxValue'] ?? 0,
      minValue: json['minValue'] ?? 0,
      value: json['value'],
      rightSlot: null,
      leftSlot: null,
      titleWidth: json['titleWidth'],
      isCanEdit: json['isCanEdit'],
      maxLines: json['maxLines'],
      suffixIcon: null,
      maxLength: json['maxLength'],
      maxPhotoCount: json['maxPhotoCount'],
    );
    if (json['rightSlot'] != null && json['rightSlot'].startsWith('image:')) {
      String url = json['rightSlot'].substring(6);
      model.rightSlot = Image.asset(url);
    } else if (json['rightSlot'] != null && json['rightSlot'].startsWith('text:')) {
      String text = json['rightSlot'].substring(5);
      model.rightSlot = Text(
        text,
        style: const TextStyle(fontSize: 14, color: Colour.otherTextColor),
      );
    } else {
      model.rightSlot = null;
    }
    if (json['leftSlot'] != null && json['leftSlot'].startsWith('image:')) {
      String url = json['leftSlot'].substring(6);
      model.leftSlot = Image.asset(url);
    } else if (json['leftSlot'] != null && json['leftSlot'].startsWith('text:')) {
      String text = json['leftSlot'].substring(5);
      model.leftSlot = Text(
        text,
        style: const TextStyle(fontSize: 14, color: Colour.otherTextColor),
      );
    } else {
      model.leftSlot = null;
    }
    if (json['suffixIcon'] != null && json['suffixIcon'].startsWith('image:')) {
      String url = json['suffixIcon'].substring(6);
      model.suffixIcon = Image.asset(url);
    } else if (json['suffixIcon'] != null && json['suffixIcon'].startsWith('text:')) {
      String text = json['suffixIcon'].substring(5);
      model.suffixIcon = Text(
        text,
        style: const TextStyle(fontSize: 14, color: Colour.otherTextColor),
      );
    } else {
      model.suffixIcon = null;
    }
    return model;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['isRequired'] = isRequired;
    data['title'] = title;
    data['subTitle'] = subTitle;
    data['hintTextString'] = hintTextString;
    data['keyboardType'] = keyboardType;
    data['type'] = type;
    data['selectList'] = selectList;
    data['maxValue'] = maxValue;
    data['minValue'] = minValue;
    data['value'] = value;
    data['rightSlot'] = rightSlot;
    data['leftSlot'] = leftSlot;
    data['titleWidth'] = titleWidth;
    data['isCanEdit'] = isCanEdit;
    data['maxLines'] = maxLines;
    data['suffixIcon'] = suffixIcon;
    data['maxLength'] = maxLength;
    data['maxPhotoCount'] = maxPhotoCount;
    return data;
  }

  Widget formItemWidgetForWeb(String urlString) {
    if (urlString.startsWith('image:')) {
      String url = urlString.substring(6);
      return Image.asset(url);
    } else if (urlString.startsWith('text:')) {
      String text = urlString.substring(5);
      return Text(
        text,
        style: const TextStyle(fontSize: 14, color: Colour.otherTextColor),
      );
    } else {
      return Container();
    }
  }
}
